from src import app, CustomBlueprint

from src.baseapp.controller.projectService.projectController import ProjectController
from src.baseapp.controller.projectService.versionController import VersionController

from flask import Response
import json

from src.baseapp.requestVerify.BaseRequestVerify import *
from src.baseapp.requestVerify.projectService.projectRequestVerify import *

pj = CustomBlueprint('projectServerView', __name__, url_prefix='/project')


@pj.route('/project', methods=['POST'], header_verify=HeadersSchema, body_verify=AddProjectBodySchema)
def add_project(request_obj):
    with ProjectController() as projectController:
        result = projectController.add_project(request_obj.body.to_dict)
        resp = Response(json.dumps((result[1])), status=result[0], mimetype='application/json')
    return resp


@pj.route('/projectList', methods=['GET'], header_verify=HeadersSchema, body_verify=ProjectListBodySchema)
def get_project_list(request_obj):
    with ProjectController() as projectController:
        result = projectController.get_project_list(request_obj.body.to_dict)
        resp = Response(json.dumps((result[1])), status=result[0], mimetype='application/json')
    return resp


@pj.route('/project', methods=['GET'], header_verify=HeadersSchema, body_verify=ProjectInfoBodySchema)
def get_project_by_id(request_obj):
    with ProjectController() as projectController:
        result = projectController.get_project_by_id(request_obj.body.project_id)
        resp = Response(json.dumps((result[1])), status=result[0], mimetype='application/json')
    return resp


@pj.route('/project', methods=['PUT'], header_verify=HeadersSchema, body_verify=UpdateProjectBodySchema)
def update_project(request_obj):
    with ProjectController() as projectController:
        result = projectController.update_project(request_obj.body.project_id, request_obj.body.to_dict)
        resp = Response(json.dumps((result[1])), status=result[0], mimetype='application/json')
    return resp


@pj.route('/project', methods=['DELETE'], header_verify=HeadersSchema, body_verify=ProjectInfoBodySchema)
def delete_project(request_obj):
    with ProjectController() as projectController:
        result = projectController.delete_project(request_obj.body.project_id)
        resp = Response(json.dumps((result[1])), status=result[0], mimetype='application/json')
    return resp


@pj.route('/version', methods=['POST'], header_verify=HeadersSchema, body_verify=AddVersionBodySchema)
def add_version(request_obj):
    with VersionController() as version:
        result = version.add_version(request_obj.body.project_id, request_obj.body.to_dict)
        resp = Response(json.dumps((result[1])), status=result[0], mimetype='application/json')
    return resp


@pj.route('/version', methods=['DELETE'], header_verify=HeadersSchema, body_verify=DeleteVersionBodySchema)
def delete_version(request_obj):
    with VersionController() as version:
        result = version.delete_version(request_obj.body.version_id)
        resp = Response(json.dumps((result[1])), status=result[0], mimetype='application/json')
    return resp


@pj.route('/version', methods=['PUT'], header_verify=HeadersSchema, body_verify=UpdateVersionBodySchema)
def update_version(request_obj):
    with VersionController() as version:
        result = version.update_version(request_obj.body.version_id, request_obj.body.to_dict)
        resp = Response(json.dumps((result[1])), status=result[0], mimetype='application/json')
    return resp


@pj.route('/version', methods=['GET'], header_verify=HeadersSchema, body_verify=VersionInfoBodySchema)
def get_version(request_obj):
    with VersionController() as version:
        result = version.get_version_by_id(request_obj.body.version_id)
        resp = Response(json.dumps((result[1])), status=result[0], mimetype='application/json')
    return resp


@pj.route('/versionList', methods=['GET'], header_verify=HeadersSchema, body_verify=VersionListBodySchema)
def get_version_list(request_obj):
    with VersionController() as version:
        result = version.get_version_list(request_obj.body.project_id, request_obj.body.to_dict)
        resp = Response(json.dumps((result[1])), status=result[0], mimetype='application/json')
    return resp


app.register_blueprint(pj)